package org.jsystemtest.tests_proj;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import jsystem.framework.FrameworkOptions;
import jsystem.framework.JSystemProperties;
import jsystem.framework.report.ExtendTestListener;
import jsystem.framework.report.TestInfo;
import jsystem.framework.report.TestReporter;
import jsystem.framework.scenario.JTestContainer;
import jsystem.framework.scenario.flow_control.AntForLoop;
import junit.framework.AssertionFailedError;
import junit.framework.Test;
import au.com.bytecode.opencsv.CSVWriter;

public class CsvReporter implements TestReporter, ExtendTestListener {

	private CSVWriter writer;
	private StringBuilder reportStr = new StringBuilder();
	private boolean failed = false;
	private int index = 0;
	private String methodName;

	public CsvReporter() {
		initReport();
	}

	private void initReport() {
		if (writer != null) {
			try {
				writer.close();
			} catch (IOException e) {
			}
		}
		writer = null;
		try {
			final String currentLogFolder = JSystemProperties.getInstance().getPreference(FrameworkOptions.LOG_FOLDER)
					+ File.separator + "current";
			File csvFile = new File(currentLogFolder + File.separator + "csv_report" + (++index) + ".csv");
			if (csvFile.exists()) {
				csvFile.delete();
			}
			writer = new CSVWriter(new FileWriter(csvFile), ',');
		} catch (IOException e) {
			System.out.println("Failed creating CSV report");
		}
		writer.writeNext(new String[] { "test name", "pass", "reports" });

	}

	public static void main(String[] args) throws Exception {
		CSVWriter writer = new CSVWriter(new FileWriter("csv_report.csv"), ',');
		writer.writeNext(new String[] { "cell1", "cell2", "cell3" });
		writer.close();

	}

	@Override
	public void addError(Test test, Throwable t) {
		failed = true;

	}

	@Override
	public void addFailure(Test test, AssertionFailedError t) {
		failed = true;

	}

	@Override
	public void endTest(Test test) {
		writer.writeNext(new String[] { methodName, Boolean.toString(!failed), reportStr.toString() });
		failed = false;

	}

	@Override
	public void startTest(Test test) {

	}

	@Override
	public void initReporterManager() throws IOException {

	}

	@Override
	public boolean asUI() {
		return false;
	}

	@Override
	public void report(String title, String message, boolean isPass, boolean bold) {
		reportStr.append("\"").append(title).append("\"");

	}

	@Override
	public void report(String title, String message, int status, boolean bold) {
		reportStr.append("\"").append(title).append("\"");
	}

	@Override
	public String getName() {
		return "CsvReporter";
	}

	@Override
	public void init() {
		initReport();
	}

	@Override
	public void addWarning(Test test) {
		// TODO Auto-generated method stub

	}

	@Override
	public void startTest(TestInfo testInfo) {
		methodName = testInfo.methodName;
		reportStr = new StringBuilder();
		failed = false;
	}

	@Override
	public void endRun() {
		try {
			writer.close();
		} catch (IOException e) {
			System.out.println("Failed closing CSV report");
		}

	}

	@Override
	public void startLoop(AntForLoop loop, int count) {
		// TODO Auto-generated method stub

	}

	@Override
	public void endLoop(AntForLoop loop, int count) {
		// TODO Auto-generated method stub

	}

	@Override
	public void startContainer(JTestContainer container) {
		// TODO Auto-generated method stub

	}

	@Override
	public void endContainer(JTestContainer container) {
		// TODO Auto-generated method stub

	}

}
